<template>
  <div class="xtx-checkbox" @click="change">
    <i class="iconfont icon-checked" v-if="checked"></i>
    <i class="iconfont icon-unchecked" v-else></i>
    <span><slot></slot></span>
  </div>
</template>
<script>
import { useVModel } from '@vueuse/core'
export default {
  name: 'XtxCheckbox',
  // 为了支持双向绑定，父元素赋值，子元素emit让父元素修改
  props: {
    modelValue: {
      type: Boolean,
      default: false
    }
  },
  setup(props, { emit }) {
    const checked = useVModel(props, 'modelValue', emit)
    const change = () => {
      const value = !checked.value
      checked.value = value // emit('update:checked', !checked.value)
      // 触发change事件
      emit('change', value) // 父元素不一定要注册change事件，如果注册了就可以触发
    }
    return {
      checked,
      change
    }
  }
}
</script>
<style scoped lang="less">
.xtx-checkbox {
  display: inline-block;
  margin-right: 2px;
  .icon-checked {
    color: @xtxColor;
    ~ span {
      color: @xtxColor;
    }
  }
  i {
    position: relative;
    top: 1px;
  }
  span {
    margin-left: 2px;
  }
}
</style>
